﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace PharmFilter
{
    public partial class PharmFilter : Form
    {
        public PharmFilter()
        {
            InitializeComponent();
        }

        public string namechcol;
        public string sortcoumn;

        private void Form3_Load(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.DataSource != null)
                {
                    dataGridView1.Sort(dataGridView1.Columns[sortcoumn], ListSortDirection.Ascending);
                    HideShowRows();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            HideShowRows();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            CurrencyManager cm = (CurrencyManager)dataGridView1.BindingContext[dataGridView1.DataSource];                  
            cm.SuspendBinding();
            int select = 0;
            int filter = 0;
            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                if (r.Visible)
                {
                    dataGridView1[namechcol, r.Index].Value = true;
                    r.DefaultCellStyle.BackColor = Color.Red;
                    select++;
                    filter++;
                }
                else
                {
                    if (Convert.ToBoolean(dataGridView1[namechcol, r.Index].Value)) select++;
                }
            }
            cm.ResumeBinding();
            ToolStripItemsUpd(dataGridView1.Rows.Count, select, filter);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            CurrencyManager cm = (CurrencyManager)dataGridView1.BindingContext[dataGridView1.DataSource];
            cm.SuspendBinding();
            int select = 0;
            int filter = 0;
            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                if (r.Visible)
                {
                    dataGridView1[namechcol, r.Index].Value = false;
                    r.DefaultCellStyle.BackColor = SystemColors.ControlLightLight;
                    filter++;
                }
                if (Convert.ToBoolean(dataGridView1[namechcol, r.Index].Value))
                {
                    select++;
                }
            }
            cm.ResumeBinding();
            ToolStripItemsUpd(dataGridView1.Rows.Count, select, filter);
        }

        private void HideShowRows()
        {
            CurrencyManager cm = (CurrencyManager)dataGridView1.BindingContext[dataGridView1.DataSource];
            cm.SuspendBinding();
            int select = 0;
            int filter = 0;
            foreach (DataGridViewRow r in dataGridView1.Rows)
            {
                r.Visible = true;
                r.DefaultCellStyle.BackColor = SystemColors.ControlLightLight;

                if (!Regex.Match(Convert.ToString(dataGridView1[dataGridView1.SortedColumn.Name, r.Index].Value), textBox1.Text, RegexOptions.IgnoreCase).Success)
                    r.Visible = false;

                if (checkBox1.Checked)
                    if (!Convert.ToBoolean(dataGridView1[namechcol, r.Index].Value)) r.Visible = false;

                if (Convert.ToBoolean(dataGridView1[namechcol, r.Index].Value))
                {
                    select++;
                    r.DefaultCellStyle.BackColor = Color.Red;
                }
                if (r.Visible) filter++;
            }
            cm.ResumeBinding();
            ToolStripItemsUpd(dataGridView1.Rows.Count, select, filter);
        }

        private void ToolStripItemsUpd(int all, int select, int filter)
        {
            statusStrip1.Items.Clear();
            statusStrip1.Items.Add("Всего строк " + all.ToString());
            statusStrip1.Items.Add("Отфильтровано " + filter.ToString());
            statusStrip1.Items.Add("Выбранно " + select.ToString());
        }

        private void dataGridView1_CurrentCellDirtyStateChanged(object sender, EventArgs e)
        {
            if (dataGridView1.IsCurrentCellDirty)
                dataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit);
        }

        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            string s = statusStrip1.Items[statusStrip1.Items.Count - 1].Text;
            int i = Convert.ToInt32(s.Replace("Выбранно ", ""));
            if (e.ColumnIndex == dataGridView1.Columns[namechcol].Index)
                if ((bool)dataGridView1[e.ColumnIndex, e.RowIndex].Value == true)
                {
                    dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = Color.Red;
                    i++;
                }
                else
                {
                    dataGridView1.Rows[e.RowIndex].DefaultCellStyle.BackColor = SystemColors.ControlLightLight;
                    i--;
                }
            statusStrip1.Items.Remove(statusStrip1.Items[statusStrip1.Items.Count - 1]);
            statusStrip1.Items.Add("Выбранно " + i.ToString());
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            HideShowRows();
        }

    }
}
